from datetime import datetime

import arrow as arrow

from db_init import db
import sqlalchemy as sa
from datetime import datetime

user_role = db.Table(
    'user_role',
    db.Column('user_id', db.ForeignKey('user.id'), primary_key=True),
    db.Column('role_id', db.ForeignKey('role.id'), primary_key=True)
)


# 用户
class User(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    login_time = sa.Column(sa.DateTime, default=datetime.now)
    name = sa.Column(sa.String(16))
    pwd = sa.Column(sa.String(50))
    phone = sa.Column(sa.String(11))
    img = sa.Column(sa.String(1000),
                    default='https://img2.baidu.com/it/u=2859542338,3761174075&fm=253&fmt=auto&app=138&f=JPEG?w=501&h=500')
    money = sa.Column(sa.DECIMAL(8, 2), default=0)
    shop = db.relationship("Shop", backref='user')
    check = db.relationship('CheckIn', uselist=False, backref='user')
    role_id = db.relationship("Role", secondary=user_role, backref='user')


# 角色表
class Role(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(16))
    # power_id = db.Column(db.Integer, db.ForeignKey('power.id'))


power_role = db.Table(
    'power_role',
    db.Column('power_id', db.ForeignKey('power.id'), primary_key=True),
    db.Column('role_id', db.ForeignKey('role.id'), primary_key=True)
)


# 权限表
class Power(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(16))
    role = db.relationship("Role", secondary=power_role, backref=db.backref('power'))
    url = sa.Column(sa.String(1024))
    method = sa.Column(sa.String(16))


class CheckIn(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    status = db.Column(sa.Boolean, default=0)
    check_date = db.Column(sa.DATE)


# 商品和小料多对多
goods_small = db.Table(
    'goods_small',
    db.Column('good_id', db.ForeignKey('goods.id'), primary_key=True),
    db.Column('small_id', db.ForeignKey('small.id'), primary_key=True)
)

# 商品和商户多对多
goods_business = db.Table(
    'goods_business',
    db.Column('good_id', db.ForeignKey('goods.id'), primary_key=True),
    db.Column('business_id', db.ForeignKey('business.id'), primary_key=True),
)

# 成品和小料多对多
finish_small = db.Table(
    'finish_small',
    db.Column('small_id', db.ForeignKey('small.id'), primary_key=True),
    db.Column('finish_id', db.ForeignKey('finish.id'), primary_key=True)
)


# 类别
class Cate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = sa.Column(sa.String(10))
    good = db.relationship("Goods", backref='cate')


# 商品
class Goods(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = sa.Column(sa.String(30))
    price = sa.Column(sa.Integer)
    price_dis = sa.Column(sa.Integer)
    num = sa.Column(sa.Integer)
    img = sa.Column(sa.String(1000))
    content = sa.Column(sa.String(500))
    cate_id = sa.Column(sa.Integer, sa.ForeignKey('cate.id'))
    small = db.relationship("Small", secondary=goods_small, backref=db.backref('goods'))
    business = db.relationship("Business", secondary=goods_business, backref=db.backref('goods'))


# 小料
class Small(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = sa.Column(sa.String(30))
    price = sa.Column(sa.Integer)
    finish = db.relationship("Finish", secondary=finish_small, backref=db.backref('small'))


# 商家
class Business(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = sa.Column(sa.String(30))
    addr = sa.Column(sa.String(100))
    open = sa.Column(sa.Boolean, default=False)
    tel = sa.Column(sa.String(11))
    order = db.relationship("Order", backref='business')


# 购物车
class Shop(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = sa.Column(sa.String(30))
    price = sa.Column(sa.Integer)
    num = sa.Column(sa.Integer)
    user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
    finish = db.relationship("Finish", backref='shop')


# 订单
class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    addr = sa.Column(sa.String(100))
    state = sa.Column(sa.Boolean, default=False)
    oid = sa.Column(sa.Integer)
    total = sa.Column(sa.Integer)
    num = sa.Column(sa.Integer)
    remark = sa.Column(sa.String(100))
    business_id = sa.Column(sa.Integer, sa.ForeignKey('business.id'))
    shop_id = sa.Column(sa.Integer, sa.ForeignKey('shop.id'))
    finish = db.relationship("Finish", backref='order')


# 成品
class Finish(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    good_id = sa.Column(sa.Integer, sa.ForeignKey('goods.id'))
    order_id = sa.Column(sa.Integer, sa.ForeignKey('order.id'))
    shop_id = sa.Column(sa.Integer, sa.ForeignKey('shop.id'))


# 日志
class Log(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
    url = sa.Column(sa.String(1024))
    mehtes = sa.Column(sa.String(32))
    create_t = sa.Column(sa.Integer)
